//
//  HQAppUtils.m
//  VirtualCurrency
//
//  Created by apple on 2021/7/5.
//

#import "HQAppUtils.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"

@implementation HQAppUtils
//关闭键盘
+ (void)closeKeyBorad
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

//弹出操作错误信息提示框
+ (void)showAlertViewTitle:(NSString*)title Message:(NSString *)message
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* action = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [[[UIApplication sharedApplication]keyWindow].rootViewController presentViewController:alert animated:YES completion:nil];
}

//弹出操作错误信息提示框,finish为点击确定按钮回调事件。
+ (void)showAlertViewTitle:(NSString *)title Message:(NSString *)message actionTitle:(NSString*)actionTitle finish:(void (^)(void))finish
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* action = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (finish)
            finish();
    }];
  // UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    //[alert addAction:cancelAction];
    
    [[[UIApplication sharedApplication]keyWindow].rootViewController presentViewController:alert animated:YES completion:nil];
}

//弹出操作错误信息提示框,finish为点击确定按钮回调事件。
+ (void)showAlertViewTitle:(NSString *)title Message:(NSString *)message sureTitle:(NSString*)sureTitle cancelTitle:(NSString*)cancelTitle finish:(void (^)(void))finish
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* action = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        finish();
    }];
     UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [alert addAction:cancelAction];
    
    [[[UIApplication sharedApplication]keyWindow].rootViewController presentViewController:alert animated:YES completion:nil];
}

+ (void)showActionSheetViewTitle:(NSString *)title Message:(NSString *)message sureTitle:(NSString*)sureTitle cancelTitle:(NSString*)cancelTitle finish:(void (^)(void))finish
{
    NSArray* ary = [message componentsSeparatedByString:@"@"];
    UIAlertActionStyle style;
    if(ary.count > 1){
        style = UIAlertActionStyleDestructive;
    }else{
        style = UIAlertActionStyleDefault;
    }
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:[ary lastObject] preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction* action = [UIAlertAction actionWithTitle:sureTitle style:style handler:^(UIAlertAction * _Nonnull action) {
        finish();
    }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [alert addAction:cancelAction];
    
    [[[UIApplication sharedApplication]keyWindow].rootViewController presentViewController:alert animated:YES completion:nil];
}



/******************** SVProgressHUD *********************/
+ (void)showSuccessMessage:(NSString *)message
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showSuccessWithStatus:message];
}

+ (void)showErrorMessage:(NSString *)message
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showErrorWithStatus:message];
}

+ (void)showProgressMessage:(NSString *) message
{
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showWithStatus:message];
}

+ (void)showProgressMessageWithNotAllowTouch:(NSString *)message
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD showWithStatus:message];
}

+ (void)showInfoMessage:(NSString *)message
{
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showInfoWithStatus:message];
}

+ (void)dismissHUD
{
    [SVProgressHUD dismiss];
}

//时间戳转化成字符串类型日期
+ (NSString*)intgerDateForStringDateWithFormatter:(NSString *)formatter timeInterval:(NSTimeInterval)timeInterval
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:formatter];
    return [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeInterval]];
}

/**
 获取当前日期之前的某个日期
 
 @param style 日期类型
 @param format 日期格式
 @param poor 差
 @return 日期时间段
 */
+ (NSString*)getCurrentDateBeforeWith:(beforeDateStyle)style format:(NSString*)format poor:(NSInteger)poor
{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
    switch (style) {
        case dateBeforeStyleWithDay:
            [lastMonthComps setDay:poor];
            break;
        case dateBeforeStyleWithWeek:
        {
            if (poor < 0) {
                [lastMonthComps setDay:-(ABS(poor) * 7 - 1)];
            }else{
                [lastMonthComps setDay:poor * 7 - 1];
            }
        }
            break;
        case dateBeforeStyleWithMonth:
            [lastMonthComps setMonth:poor];
            break;
        case dateBeforeStyleWithYear:
            [lastMonthComps setYear:poor];
        default:
            break;
    }
    NSDate *newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];
    NSString *dateStr = [formatter stringFromDate:newdate];
    
    return dateStr;
}

/**
 计算两个时间差
 
 @param timeOne 时间一
 @param timeTwo 时间二
 @return 返回差
 */
+ (NSInteger)compareWithTwoTimeByJetLag:(NSDate *)timeOne timeTwo:(NSDate*)timeTwo
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *d = [cal components:unitFlags fromDate:timeOne toDate:timeTwo options:0];
    NSInteger sec = [d day];
    return sec;
}

/**
 通过时间戳和格式显示时间
 
 @param timestamp 时间戳
 @param formatter 日期格式
 
 @return 日期字符串
 */
+ (NSString*)getStringWithTimestamp:(NSTimeInterval)timestamp formatter:(NSString *)formatter
{
    if ([NSString stringWithFormat:@"%@", @(timestamp)].length == 13)
        timestamp /= 1000.0f;
    
    NSDate* timestampDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    NSString *strDate = [dateFormatter stringFromDate:timestampDate];
    
    return strDate;
}

/**
 网络状态

 @return 提示语
 */
+(NSString *)returnNetworkState{
    __block NSString *network;
    AFNetworkReachabilityManager* recachManager = [AFNetworkReachabilityManager sharedManager];
    switch ([recachManager networkReachabilityStatus]) {
        case AFNetworkReachabilityStatusUnknown: // 未知网络
            network = @"未知网络";
            break;
        case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
            network = @"没有网络(断网)";
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
            network = @"移动网络";
            break;
            
        case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
            network = @"WIFI";
            break;
        default:
            break;
    }
    return network;
}

/**
 网络模式提示
 */
+ (void)monitorNetworkStateHUD
{
    AFNetworkReachabilityManager* recachManager = [AFNetworkReachabilityManager sharedManager];
    [recachManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch(status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
              //  [HQHUDView showMessage:@"未知网络"];
                break;
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
              //  [HQHUDView showMessage:@"没有网络(断网)"];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
               // [HQHUDView showMessage:@"移动网络"];
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
               // [HQHUDView showMessage:@"WIFI"];
                break;
        }
    }];
    // 3.开始监控
    [recachManager startMonitoring];
}

/**
 替换nil
 
 @param string 需要处理的字符
 @param replacString 用此字符替换
 @return 替换好的字符
 */
+ (NSString*)replaceNilWithString:(NSString *)string forString:(NSString *)replacString
{
    if ([string isKindOfClass:[NSNull class] ] || string.length <= 0) {
        return replacString;
    }
    return string;
}

//释放
- (void)dealloc
{
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
}

@end
